import config
import consumer
from builder import Builder

from oslo_config import cfg
from oslo_log import log as logging
import time


LOG = logging.getLogger(__name__)

class Manager(object):

    def __init__(self):
        config.load_config()

        self.cnsm = None
        if cfg.CONF.csv_file:
            self.cnsm = consumer.get_consumer(
                'csv', csv_file=cfg.CONF.csv_file
            )

        self.builder = Builder()

    def recreate_loadbalancers(self):
        lb_ids = self.cnsm.get_loadbalancer_ids()
        LOG.info("Get loadbalancers %s." %
                 lb_ids)
        self.builder.rebuild_loadbalancers(
            lb_ids
        )

    def recreate_listeners(self):
        ls_ids = self.cnsm.get_listener_ids()
        LOG.info("Get listeners %s." %
                 ls_ids)
        self.builder.rebuild_listeners(
            ls_ids
        )

    def recreate_pools(self):
        pl_ids = self.cnsm.get_pool_ids()
        LOG.info("GEt pools %s ." %
                 pl_ids)
        self.builder.rebuild_pools(
            pl_ids
        )

    def recreate_members(self):
        mb_ids = self.cnsm.get_member_ids()
        LOG.info("Get members %s ." %
                 mb_ids)
        self.builder.rebuild_members(
            mb_ids
        )

    def recreate_healthmonitor(self):
        hm_ids = self.cnsm.get_healthmonitor_ids()
        LOG.info("Get healthmonitors %s ." %
                 hm_ids)
        self.builder.rebuild_healthmonitors(
            hm_ids
        )

    def recreate_l7policy(self):
        l7policy_ids = self.cnsm.get_l7policy_ids()
        LOG.info("Get l7policy %s ." %
                 l7policy_ids)
        self.builder.rebuild_l7policies(l7policy_ids)

    def recreate_l7rule(self):
        # recreate l7rule need l7policy_id and l7rule_id
        pass

    def start(self):
        self.recreate_loadbalancers()

        self.recreate_pools()
        self.recreate_members()
        self.recreate_healthmonitor()
        self.recreate_listeners()

        self.recreate_l7policy()


if __name__ == "__main__":
    manager = Manager()
    manager.start()
